package com.sts.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;
//Redis 黑名单
@Component
public class TokenBlackList {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    // 添加Token到黑名单，使用实际过期时间
    public void add(String token, long seconds) {
        redisTemplate.opsForValue().set("blacklist:" + token, "1", seconds, TimeUnit.SECONDS);
    }

    // 检查Token是否在黑名单中
    public boolean contains(String token) {
        return Boolean.TRUE.equals(redisTemplate.hasKey("blacklist:" + token));
    }

    // 添加一个新方法，直接使用Token计算剩余时间
    public void addToken(String token, JwtUtil jwtUtil) {
        long remainingSeconds = jwtUtil.getRemainingSeconds(token);
        this.add(token, remainingSeconds);
    }
}